package com.wzs.easytobuy.controller;

import com.wzs.easytobuy.model.*;
import com.wzs.easytobuy.service.IProductService;
import com.wzs.easytobuy.utils.PageModel;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.util.locale.provider.FallbackLocaleProviderAdapter;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author miaomiao
 * @version 1.0.0
 * @Description 处理商品模块相关业务的控制器
 * @createTime 2021-06-25- 19:21:00
 */
@Controller
@RequestMapping("/product/")
public class ProductController {

    @Resource
    private IProductService iProductService;

    @RequestMapping("CategoryList.do")
    public String CategoryList(HttpSession session, Model model, String id) {

        //System.out.println(id);
        System.out.println("实现从主页根据商品分类点击进入商品信息页");
        ProductType p = iProductService.selectByPrimaryKey(id);

        //System.out.println(p);

        session.setAttribute("productTypeId", id);
        model.addAttribute("id", id);
        model.addAttribute("name", p.getName());
        model.addAttribute("proColumn", p.getProColumn());

        return "CategoryList";
    }

    @RequestMapping("CategoryList2.do")
    public String CategoryList2(HttpSession session, Model model, String productTypeName) {

        System.out.println("实现从主页根据商品分类点击进入商品信息页");
        ProductType p = iProductService.selectByName(productTypeName);

        session.setAttribute("productTypeId", p.getId());
        model.addAttribute("id", p.getId());
        model.addAttribute("name", p.getName());
        model.addAttribute("proColumn", p.getProColumn());

        return "CategoryList";
    }

    @RequestMapping("categoryList.do")
    @ResponseBody
    public List<ProductType> categoryList(String proColumn, String name) {
        System.out.println("执行品牌信息展示的操作");
        return iProductService.selectByPcAndName(proColumn, name);
    }

    @RequestMapping("productPageList.do")
    @ResponseBody
    public PageModel<Product> productPageList(String productTypeId, String pageNum, String pageSize, String productTypeName) {

        System.out.println("执行商品信息分类展示的操作");

        int pageNum1 = pageNum == null ? 1 : Integer.parseInt(pageNum);

        int skipCount = Integer.parseInt(pageSize) * (pageNum1 - 1);

        PageModel<Product> pageModel = new PageModel<>();
        pageModel.setPageNum(pageNum1);
        pageModel.setSkipCount(skipCount);
        pageModel.setPageSize(Integer.parseInt(pageSize));

        return iProductService.productPageList(productTypeId, pageModel, productTypeName);
    }

    @RequestMapping("detail.do")
    public String detail(Model model, HttpSession session, String id) {

        Product p = iProductService.selectById(id);
        //System.out.println(p);
        Format f = iProductService.selectFormatById(id, p.getPrice(), p.getSales());
        //System.out.println(f);

        //如果点击了商品详情,就需要添加到历史记录里
        User u = (User) session.getAttribute("loginUser");
        iProductService.addHistory(f.getId(), u.getId());

        //需要判断是否添加到我的收藏里
        Like l = iProductService.selectLikeByUidAndFid(f.getId(), u.getId());

        model.addAttribute("l", l);
        model.addAttribute("p", p);
        model.addAttribute("f", f);

        return "Product";
    }

    @RequestMapping("cleanHistory.do")
    @ResponseBody
    public boolean cleanHistory(HttpSession session) {

        User u = (User) session.getAttribute("loginUser");

        return iProductService.deleteHistory(u.getId());

    }

    @RequestMapping("showFormat.do")
    @ResponseBody
    public Map<String, Object> showFormat(String productId) {

        Map<String, Object> map = new HashMap<>();
        List<Color> colorList = iProductService.selectColorByPid(productId);
        List<Size> sizeList = iProductService.selectSizeByPid(productId);
        List<Format> formatList = new ArrayList<>();
        for (Color color : colorList) {
            for (Size size : sizeList) {
                Format format = iProductService.selectByCidAndSid(productId, color.getId(), size.getId());
                formatList.add(format);
            }
        }

        map.put("colorList", colorList);
        map.put("sizeList", sizeList);
        map.put("formatList", formatList);

        return map;
    }

    @RequestMapping("showFormat2.do")
    @ResponseBody
    public Map<String, Object> showFormat2(String productId) {

        Map<String, Object> map = new HashMap<>();
        List<Color> colorList = iProductService.selectColorByPid(productId);
        List<Size> sizeList = iProductService.selectSizeByPid(productId);
        List<Format> formatList = new ArrayList<>();
        for (Color color : colorList) {
            for (Size size : sizeList) {
                Format format = iProductService.selectByCidAndSid2(productId, color.getId(), size.getId());
                formatList.add(format);
            }
        }

        map.put("colorList", colorList);
        map.put("sizeList", sizeList);
        map.put("formatList", formatList);

        return map;
    }

    @RequestMapping("showHistory.do")
    @ResponseBody
    public List<Map<String, Object>> showHistory(String userId) {
        return iProductService.selectHistoryByUid(userId);
    }

    @RequestMapping("Order.do")
    public String Order() {
        return "Member_Order";
    }

    @RequestMapping("orderPageList.do")
    @ResponseBody
    public PageModel<Map<String, Object>> orderPageList(String userId, String pageNum, String pageSize) {

        System.out.println("执行订单分页查询操作");

        int pageNum1 = pageNum == null ? 1 : Integer.parseInt(pageNum);

        int skipCount = Integer.parseInt(pageSize) * (pageNum1 - 1);

        PageModel<Map<String, Object>> pageModel = new PageModel<>();
        pageModel.setPageNum(pageNum1);
        pageModel.setSkipCount(skipCount);
        pageModel.setPageSize(Integer.parseInt(pageSize));

        return iProductService.orderPageList(userId, pageModel);
    }

    @RequestMapping("Collect.do")
    public String Collect() {
        return "Member_Collect";
    }

    @RequestMapping("likePageList.do")
    @ResponseBody
    public PageModel<Map<String, Object>> likePageList(String userId, String pageNum, String pageSize) {

        System.out.println("执行收藏分页查询操作");

        int pageNum1 = pageNum == null ? 1 : Integer.parseInt(pageNum);

        int skipCount = Integer.parseInt(pageSize) * (pageNum1 - 1);

        PageModel<Map<String, Object>> pageModel = new PageModel<>();
        pageModel.setPageNum(pageNum1);
        pageModel.setSkipCount(skipCount);
        pageModel.setPageSize(Integer.parseInt(pageSize));

        return iProductService.likePageList(userId, pageModel);
    }

    @RequestMapping("deleteLike.do")
    @ResponseBody
    public boolean deleteLike(String id) {
        System.out.println("执行收藏商品删除操作");
        return iProductService.deleteLike(id);
    }

    @RequestMapping("addLike.do")
    @ResponseBody
    public Map<String, Object> addLike(String productId, String userId, String price, String sales) {

        System.out.println("执行添加商品到收藏列表中操作");

        try {
            boolean flag = iProductService.addLike(productId, userId, price, sales);

            Map<String, Object> map = new HashMap<>();
            map.put("success", flag);

            return map;
        } catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            Map<String, Object> map = new HashMap<>();
            map.put("success", false);
            map.put("msg", msg);

            return map;
        }
    }


}
